﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OneSystems.BusinessLogic;
using OneSystems.Data.Entities;

namespace UserInterface.HelperForms
{
    public partial class EditAccount : Form
    {
        public int AccountID { get; set; }
        public int CategoryID { get; set; }
        public int DebtorID { get; set; }
        public EditAccount()
        {
            InitializeComponent();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            Creditor creditor = (Creditor)creditorList.SelectedItem;
            Brand brand = (Brand)BrandCombo.SelectedItem;
            int? brandID = null;

            if (brand != null)
            {
                brandID = brand.BrandID;
            }
            
            DebtLogic.SaveAccount(AccountID, 
                creditor.CreditorID,
                DebtorID,CategoryID,
                brandID,  
                (DateTime)OpenDatePicker.Value, 
                (DateTime?)CloseDatePicker.Value,
                AccountNumberbox.Text, 
                decimal.Parse(InterestRateBox.Text.Replace("%", string.Empty))/100,
                decimal.Parse(CashAdvanceInterestBox.Text.Replace("%", string.Empty))/100,
                (decimal)CreditLimit.Value, 
                AccountNicknameBox.Text,MinimumPaymentBox.Value,
                decimal.Parse(PaymentPercentageBox.Text.Replace("%", string.Empty)) / 100,
                (DateTime)DueDaySelector.Value);
            this.Close();
        }

        private void EditAccount_Load(object sender, EventArgs e)
        {
            InitializeCreditorList();
            InitializeBrandList();
            if (AccountID > 0)
            {
                Account account = DebtLogic.GetAccount(AccountID);
                creditorList.SelectedItem = (object)account.Creditor;
                BrandCombo.SelectedItem = account.Brand != null ? (object)account.Brand : null;
                OpenDatePicker.Value = account.OpenDate;
                CloseDatePicker.Value = account.ClosedDate;
                AccountNumberbox.Text = account.AccountNumber;
                InterestRateBox.Text = account.InterestRate.ToString("%#0.0000");
                CashAdvanceInterestBox.Text = account.CashAdvanceRate.ToString("%#0.0000");
                AccountNicknameBox.Text = account.AccountNickname;
                CreditLimit.Value = account.CreditLimit;
                MinimumPaymentBox.Value = account.MinimumPayment;
                PaymentPercentageBox.Text = account.PaymentPercentage.ToString("%#0.0000");
                DateTime displayDate = DateTime.MinValue;
                displayDate.AddDays(account.DueDay - 1);
                DueDaySelector.Value = displayDate;

            }
        }



        private void ultraNumericEditor1_ValueChanged(object sender, EventArgs e)
        {

        }
        #region Private Methods
        private void InitializeCreditorList()
        {
            foreach (Creditor creditor in DebtLogic.Creditors)
            {
                creditorList.Items.Add(creditor);
            }            
        }
        private void InitializeBrandList()
        {
            foreach (Brand brand in DebtLogic.Brands)
            {
                BrandCombo.Items.Add(brand);
            }
        }
        #endregion
    }
}
